﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using Kenly.ChineseSegment.Core;
using Kenly.ChineseSegment.DictionaryLoader;
using Kenly.ChineseSegment.DictionaryWriter;

namespace SegmentTool
{
    public partial class frmDicFilter : Form
    {
        IDictionaryLoader _loader = new TextDictionaryLoader();
        List<string> _segmentList;

        public frmDicFilter()
        {
            InitializeComponent();
        }

        private void frmDicFilter_Load(object sender, EventArgs e)
        {

        }

        private void btnSelectDic_Click(object sender, EventArgs e)
        {
            DialogResult result = openFileDialog1.ShowDialog();
            if (result == DialogResult.Cancel || string.IsNullOrEmpty(openFileDialog1.FileName))
            {
                return;
            }

            txtDicPath.Text = openFileDialog1.FileName;

            DateTime loadStartTime = DateTime.Now;
            _segmentList = _loader.Load(txtDicPath.Text);
            TimeSpan loadSpan = DateTime.Now - loadStartTime;

            txtSegmentCount.Text = _segmentList.Count.ToString();
            txtLoadTimeSpan.Text = loadSpan.TotalMilliseconds.ToString();

            txtNewSegmentCount.Text = "";
            txtFilterTimeSpan.Text = "";
        }

        private void btnFilter_Click(object sender, EventArgs e)
        {
            DateTime filterStartTime = DateTime.Now;
            SegmentFilter.Filter(_segmentList);
            TimeSpan span = DateTime.Now - filterStartTime;

            txtNewSegmentCount.Text = _segmentList.Count.ToString();
            txtFilterTimeSpan.Text = span.TotalMilliseconds.ToString();

            TextDictionaryWriter writer = new TextDictionaryWriter();
            writer.Write(txtDicPath.Text, _segmentList);
        }
    }
}
